<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'db_connection.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_date.php';

class dao_menu {

    //Methods
    public function __construct() {
        
    }

    public function get_all() {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_menu";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }

        $db->close_connect($con);
        return $list;
    }

    public function add($menu_id, $menu_title, $menu_content, $menu_feature_title, $menu_feature_content) {
        $db = new db_connection();
        $con = $db->open_connect();

        if ($menu_id == 0) {
            $menu_key = $menu_title;
            echo "INSERT INTO tbl_menu(menu_key, menu_title, menu_content, menu_feature_title, menu_feature_content) VALUES ('" . $menu_key . "','" . $menu_content . "','" . $menu_feature_title . "', '" . $menu_feature_content . "')";
            $query = "INSERT INTO tbl_menu(menu_key, menu_title, menu_content, menu_feature_title, menu_feature_content) VALUES ('" . $menu_title . "','" . $menu_title . "','" . $menu_content . "','" . $menu_feature_title . "', '" . $menu_feature_content . "')";
        } else {
            $query = "UPDATE tbl_menu SET menu_title = '" . $menu_title . "' , menu_content = '" . $menu_content . "' , menu_feature_title = '" . $menu_feature_title . "', menu_feature_content = '" . $menu_feature_content . "' WHERE menu_id = " . $menu_id;
        }
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect($con);
        return true;
    }

    public function get_by_id($id) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_menu WHERE menu_id = " . $id;
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect($con);
        return $row;
    }

    public function delete($id) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tbl_menu WHERE menu_id = " . $id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect($con);
        return true;
    }
    
}
